package com.lczc.soa.controller;

import com.lczc.soa.domain.*;
import com.lczc.soa.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.List;

@Controller
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private DoorService doorService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private StreetService streetService;

    @Autowired
    private ApplyService applyService;

    @Autowired
    private NewsService newsService;



//    @RequestMapping("/toadd")
//    public String addUserPage(Model model){
//        model.addAttribute("user",new User());
//        return "user_add";
//    }

//    @RequestMapping("/add")
//    public String addUser(User user){
////        System.out.println("Controller:" + user.getUserName());
//        int updated = userService.saveUser(user);
//        return "redirect:/User/list";
//    }

    @RequestMapping("/list")
    public String UserList() {
        return "user_list";
    }

/*    @RequestMapping("/toapplylist")
    public String Userqpply(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();


        User current_user = (User) session.getAttribute("current_user");
        User user = userService.findUserByPrimaryKey(current_user.getUserId());
        model.addAttribute("user",user);
        return "userapply_list";


    }*/
    @RequestMapping("/upate")
    public String Userupate(@RequestParam("id") String id,Model model) {
        User user = userService.findUserByPrimaryKey(id);
        model.addAttribute("user",user);
        return "upate";
    }

    @RequestMapping("/toedit")
    public String Useredit(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();


        User current_user = (User) session.getAttribute("current_user");
        User user = userService.findUserByPrimaryKey(current_user.getUserId());
        model.addAttribute("user",user);


        return "user_edit";
    }

    @RequestMapping("/tomes")
    public String Usermes() {
        return "user_mes";
    }

    @RequestMapping("/form")
    public String UserForm(Model model, @RequestParam("id") String id) {
        User user = userService.findUserByPrimaryKey(id);
        model.addAttribute("user", user);
        return "user_form";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Boolean UserSave(User user) {
        int deleted = userService.saveUser(user);
        return deleted > 0;
    }



    @GetMapping("/api/search")
    @ResponseBody
    public SearchUserResponse searchUserResponse(@RequestParam("pageNum") Integer pageNum,
                                                 @RequestParam("pageSize") Integer pageSize) {
        return userService.findAllUser(pageNum, pageSize);
    }

    @GetMapping("/api/searchLike")
    @ResponseBody
    public SearchUserResponse searchUserResponseLike(@RequestParam("keywords") String keywords,
                                                     @RequestParam("pageNum") Integer pageNum,
                                                     @RequestParam("pageSize") Integer pageSize) {
        return userService.findUserLikeName(keywords, pageNum, pageSize);
    }

//    @RequestMapping("/edit")
//    public String EditUser(@RequestParam("id") String id, Model model){
//        User user = userService.findUserByPrimaryKey(id);
//        model.addAttribute("user",user);
//        return "user_add";
//    }

    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteUser(@RequestParam("id") String id) {
        int deleted = userService.deleteUser(id);
        return deleted > 0;
    }

    @RequestMapping("/toregister")
    public String registerPage() {
        return "news_submit";
    }

    @RequestMapping("/register")
    public String register(Model model, @ModelAttribute User user, @RequestParam("passwd") String passwd) {
        String msgg = null;
        User usered = userService.findUserByName(user.getUserName());
        if (StringUtils.isBlank(user.getUserName())) {
            msgg = "用户名不能为空";
            model.addAttribute("msgg", msgg);
            return "login";
        } else if (usered!=null){
            msgg = "用户名已存在";
            model.addAttribute("msgg", msgg);
            return "login";
        } else if (StringUtils.isBlank(user.getPassword())) {
            msgg = "密码不能为空";
            model.addAttribute("msgg", msgg);
            return "login";
        } else if (StringUtils.isBlank(passwd) || !passwd.equals(user.getPassword())) {
            msgg = "两次密码输入不一致";
            model.addAttribute("msgg", msgg);
            return "login";
        } else {
            int updated = userService.saveUser(user);
            msgg = "注册成功";
            model.addAttribute("msgg", msgg);
            return "login";
        }
    }

    @RequestMapping("/tologin")
    public String tologin() {

        return "login";
    }
    @RequestMapping("/tozhuye")
    public String tozhuye() {

        return "user_page";
    }

    @RequestMapping("/login")
    public String login(Model model, String userName, String password, HttpSession session) {
        System.out.println(userName);
        User user = userService.findUserByName(userName);
        String msg = null;
        if (user == null) {
            msg = "用户名不存在";
            model.addAttribute("msg", msg);
            return "login";
        } else {
            String pwd = user.getPassword();
            if (password != null && pwd.equals(password)) {
                msg = "登录成功";
                model.addAttribute("msg", msg);
                session.setAttribute("current_user", user);
                return "user_page";
            } else {
                msg = "密码错误";
                model.addAttribute("msg", msg);
                return "login";
            }
        }
    }

    @RequestMapping("/edit")
    public String EditUser(@RequestParam("id") String id, Model model) {
        User user = userService.findUserByName(id);
        model.addAttribute("user", user);
        return "user_edit";
    }

    @RequestMapping("/add")
    public String addUser(User user) {
//        System.out.println("Controller:" + area.getAreaName());
        int updated = userService.saveUser(user);
        return "user_edit";
    }


    @RequestMapping("/apply")
    public String apply(Apply apply) {
        int updated = userService.saveApply(apply);
        Door door = doorService.findDoorByPrimaryKey(apply.getDoorId());
        door.setUserId(apply.getUserId());
        doorService.saveDoor(door);
        return "redirect:/User/toedit";
    }

    @RequestMapping("/toapply")
    public String toApplyPage() {
        return "user_apply";
    }

    @RequestMapping("/findApply")
    @ResponseBody
    public List<Apply> findApply(@RequestParam("id") String id) {
        return applyService.findApplyByUserId(id);
    }

//    @RequestMapping("/findNews")
//    @ResponseBody
//    public List<News> findNews(@RequestParam("id") String id){ return newsService.findNewsByUserId(id);
//    }



    @RequestMapping("/findArea")
    @ResponseBody
    public List<Area> findArea() {
        return areaService.listArea();
    }

    @RequestMapping("/findStreet")
    @ResponseBody
    public List<Street> findStreet(@RequestParam("id") String id) {
        return streetService.findStreetByArea(id);
    }

    @RequestMapping("findDoor")
    @ResponseBody
    public List<Door> findDoor(@RequestParam("id") String id) {
        return doorService.findDoorByStreet(id);
    }


    @PostMapping("/upatepwd")
    @ResponseBody
    public String updatePassword(Model model,@RequestParam("userId") String userId,
                                 @RequestParam("oldPassword") String oldPassword,
                                 @RequestParam("newPassword") String newPassword,
                                 @RequestParam("rePassword") String rePassword
                                 ) {
        User user = userService.findUserByPrimaryKey(userId);
        String mesgg = null;
        if (StringUtils.isBlank(oldPassword) || StringUtils.isBlank(newPassword) || StringUtils.isBlank(rePassword)) {
            mesgg = "密码不能为空";
            model.addAttribute("mesgg", mesgg);
            return "upate";
        }else if(!user.getPassword().equals(oldPassword)){
            mesgg = "旧密码错误";
            model.addAttribute("mesgg", mesgg);
            return "upate";
        }else if (!newPassword.equals(rePassword)) {
            mesgg = "两次密码不一致";
            model.addAttribute("mesgg", mesgg);
            return "upate";
        }else {
            user.setPassword(newPassword);
            int updated = userService.saveUser(user);
        }
        return "ok";
    }

    @GetMapping("/api/searchByUser")
    @ResponseBody
    public SearchApplyResponse searchApplyResponseByUser(@RequestParam("id") String id,
                                                       @RequestParam("pageNum") Integer pageNum,
                                                       @RequestParam("pageSize") Integer pageSize){
        return applyService.findApplyByUser(id,pageNum,pageSize);
    }

    @GetMapping("/api/searchLikeByUser")
    @ResponseBody
    public SearchApplyResponse searchApplyResponseLikeByUser(@RequestParam("keywords") String keywords,
                                                         @RequestParam("id") String id,
                                                         @RequestParam("pageNum") Integer pageNum,
                                                         @RequestParam("pageSize") Integer pageSize){
        return applyService.findApplyLikeByUser(keywords,id,pageNum,pageSize);
    }
}
